package jlib.component.button
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	
	import jlib.component.core.LabelBox;
	import jlib.utils.ArrangeUtil;
	
	public class CheckBox extends Sprite {
		private var m_button:SkinButton;
		private var m_label:LabelBox;
		private var m_place:String;
		private var m_bAutoSize:Boolean;
		private var m_space:Number;
		private var m_data:*;
		
		public function CheckBox() {
			super();
			init();
		}
		
		private function init(): void {
			buttonMode = true;
			mouseChildren = false;
			
			m_space = 0;
			m_label = new LabelBox();
			m_label.setBackground(0x00ffffff);
			
			m_button = new SkinButton();
			m_button.setWrappee(this);
			m_button.setToggle(true);
			
			addChild(m_button);
			addChild(m_label);
			setPlace(CheckBoxPlace.LEFT);
			setAutoSize(true);
			
		}
		
		public function setSkin(skinName:String, skin:DisplayObject): void { 
			m_button.setSkin(skinName, skin);
			update();
		}
		
		public function setTextFormat(format:TextFormat): void {
			m_label.setFormat(format);
			update();
		}
		
		public function getFont(): String { return m_label.getFont(); }
		public function setFont($fontName:String): void {
			m_label.setFont($fontName);
			update();
		}
		
		public function getLabel(): String { return m_label.getLabel(); }
		public function setLabel(text:String): void  { 
			m_label.setLabel(text);
			update();
		}
		
		public function getFontColor():uint { return m_label.getFontColor(); }
		public function setFontColor($color:uint): void {
			m_label.setFontColor($color);
		}
		
		public function getFontSize(): uint { return m_label.getFontSize(); }
		public function setFontSize($size:uint): void {
			m_label.setFontSize($size);
		}
		
		public function getPlace(): String { return m_place; }
		
		public function setPlace($place:String): void {
			m_place = $place;
			update();
		}
		
		public function getData(): * { return m_data; }
		public function setData(data:*): void  {
			m_data = data;
		}
		
		public function setAutoSize(boolean:Boolean): void {
			m_bAutoSize = boolean;
			update();
		}
		
		public function seSpace(space:Number): void {
			m_space = space;
			update();
		}
		
		private function update(): void {
			switch(m_place){
				case CheckBoxPlace.LEFT :
					m_button.x = 0, m_button.y = 0;
					m_button.y = m_label.height/2-m_button.height/2;
					//textFormat.align = TextFormatAlign.LEFT;
					if(m_bAutoSize) m_label.setAutoSize(TextFieldAutoSize.LEFT); 
					else m_label.setAutoSize(TextFieldAutoSize.NONE);
					ArrangeUtil.horizontalSpace([m_button, m_label], m_space);
				break;
				case CheckBoxPlace.RIGHT :
					m_label.x = 0, m_label.y = 0;
					m_button.y = m_label.height/2-m_button.height/2;
					//textFormat.align = TextFormatAlign.RIGHT;
					if(m_bAutoSize) m_label.setAutoSize(TextFieldAutoSize.LEFT);
					else m_label.setAutoSize(TextFieldAutoSize.NONE);
					ArrangeUtil.horizontalSpace([m_label,m_button],m_space);
				break;
				case CheckBoxPlace.TOP :
					m_label.x = 0, m_label.y = 0;
					m_button.x = m_label.width/2-m_button.width/2;
					m_button.y = 0;
					if(m_bAutoSize) m_label.setAutoSize(TextFieldAutoSize.LEFT);
					else m_label.setAutoSize(TextFieldAutoSize.NONE);
					ArrangeUtil.verticalSpace([m_button,m_label],m_space);
					//textFormat.align = TextFormatAlign.CENTER;
				break;
				case CheckBoxPlace.BOTTOM :
					m_label.x = 0, m_label.y = 0;
					m_button.x = m_label.width/2-m_button.width/2;
					m_button.y = 0;
					if(m_bAutoSize) m_label.setAutoSize(TextFieldAutoSize.LEFT);
					else m_label.setAutoSize(TextFieldAutoSize.NONE);
					ArrangeUtil.verticalSpace([m_label,m_button],m_space);
				break;
			}
		}
		
		private function changeState(e:Event): void {
			if(isCheck()) decheck();
			else check();
		}
		
		public function check(): void {
			if(m_button.isSelected()) return ; 
			m_button.toggle();
			dispatchEvent(new Event(Event.CHANGE));
		}
		
		public function decheck(): void {
			if(!m_button.isSelected()) return;
			m_button.toggle();
			dispatchEvent(new Event(Event.CHANGE));
		}
		
		public function isCheck(): Boolean { return m_button.isSelected(); }
		
	}//c
}